package com.svse.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.svse.dao.ProductinfoDAO;
import com.svse.vo.Productinfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * Productinfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.svse.vo.Productinfo
 * @author MyEclipse Persistence Tools
 */

@Repository("productinfoDAO")
@Transactional
public class ProductinfoDAOImpl extends HibernateDaoSupport implements ProductinfoDAO {
	private static final Logger log = LoggerFactory
			.getLogger(ProductinfoDAOImpl.class);
	protected void initDao() {
		// do nothing
	}
	@Autowired
	public void setSuperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#save(com.svse.vo.Productinfo)
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(Productinfo transientInstance) {
		log.debug("saving Productinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#delete(com.svse.vo.Productinfo)
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public void delete(Productinfo persistentInstance) {
		log.debug("deleting Productinfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#findById(java.lang.Integer)
	 */
	@Transactional(readOnly=true)
	public Productinfo findById(java.lang.Integer id) {
		log.debug("getting Productinfo instance with id: " + id);
		try {
			Productinfo instance = (Productinfo) getHibernateTemplate().get(
					"com.svse.vo.Productinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#findByExample(com.svse.vo.Productinfo)
	 */
	@Transactional(readOnly=true)
	public List findByExample(Productinfo instance) {
		log.debug("finding Productinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Transactional(readOnly=true)
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Productinfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Productinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#findByProducename(java.lang.Object)
	 */
	@Transactional(readOnly=true)
	public List findByProducename(Object producename) {
		return findByProperty(PRODUCENAME, producename);
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#findByProduceprice(java.lang.Object)
	 */
	@Transactional(readOnly=true)
	public List findByProduceprice(Object produceprice) {
		return findByProperty(PRODUCEPRICE, produceprice);
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#findByProducenote(java.lang.Object)
	 */
	@Transactional(readOnly=true)
	public List findByProducenote(Object producenote) {
		return findByProperty(PRODUCENOTE, producenote);
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#findByProducepic(java.lang.Object)
	 */
	@Transactional(readOnly=true)
	public List findByProducepic(Object producepic) {
		return findByProperty(PRODUCEPIC, producepic);
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#findAll()
	 */
	@Transactional(readOnly=true)
	public List findAll() {
		log.debug("finding all Productinfo instances");
		try {
			String queryString = "from Productinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#merge(com.svse.vo.Productinfo)
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public Productinfo merge(Productinfo detachedInstance) {
		log.debug("merging Productinfo instance");
		try {
			Productinfo result = (Productinfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#attachDirty(com.svse.vo.Productinfo)
	 */
	public void attachDirty(Productinfo instance) {
		log.debug("attaching dirty Productinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.svse.vo.ProductinfoDAO#attachClean(com.svse.vo.Productinfo)
	 */
	public void attachClean(Productinfo instance) {
		log.debug("attaching clean Productinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ProductinfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (ProductinfoDAO) ctx.getBean("ProductinfoDAO");
	}
	
	@Transactional(readOnly=true)
	public List findAllByPage(int beginIndex, int maxLength) {
		Session session = this.getSession();
		Criteria criteria = session.createCriteria(Productinfo.class);
		criteria.addOrder(Order.desc("produceId"));
		criteria.setFirstResult(beginIndex);
		criteria.setMaxResults(maxLength);
		List list = criteria.list();
		return list;
	}
}